package org.example.entity;

import java.sql.Timestamp;

public class Blog {
    private String blogId;
    private String userId;
    private String title;
    private String content;
    private Timestamp createdAt;
    private Timestamp updatedAt;
    private int viewCount;
    private int status; // 1:公开, 0:私密

    // Getters and Setters
    public String getBlogId() {
        return blogId;
    }

    public void setBlogId(String blogId) {
        this.blogId = blogId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Timestamp getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Timestamp createdAt) {
        this.createdAt = createdAt;
    }

    public Timestamp getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Timestamp updatedAt) {
        this.updatedAt = updatedAt;
    }

    public int getViewCount() {
        return viewCount;
    }

    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
    public String toJsonString() {
        return "{" +
                "\"blogId\":\"" + blogId + "\"," +
                "\"userId\":\"" + userId + "\"," +
                "\"title\":\"" + escapeJson(title) + "\"," +
                "\"content\":\"" + escapeJson(content) + "\"," +
                "\"createdAt\":\"" + createdAt + "\"," +
                "\"updatedAt\":" + (updatedAt != null ? "\"" + updatedAt + "\"" : "null") + "," +
                "\"viewCount\":" + viewCount + "," +
                "\"status\":" + status +
                "}";
    }

    private String escapeJson(String input) {
        if (input == null) return "";
        return input.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }

}